<template>
  <div>
    <el-dialog
      v-model="props.isShow"
      :title="props.isAdd ? '添加菜单' : '编辑菜单'"
      center
      :before-close="cancel"
    >
      <el-form :model="menuForm" :rules="rules" ref="menuRef">
        <el-form-item prop="title" label="菜单名称" :label-width="formLabelWidth">
          <el-input v-model="menuForm.title" autocomplete="off" />
        </el-form-item>
        <el-form-item prop="pid" label="上级菜单" :label-width="formLabelWidth">
          <el-select v-model="menuForm.pid" placeholder="请选择">
            <el-option label="顶级菜单" :value="0" />
            <el-option
              v-for="item in $store.getters['menu/getMenu']"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="菜单类型" :label-width="formLabelWidth">
          <el-radio-group v-model="menuForm.type">
            <el-radio :label="1" size="large">目录</el-radio>
            <el-radio :label="2" size="large">菜单</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="菜单图标"
          :label-width="formLabelWidth"
          v-if="menuForm.type == 1"
        >
          <!-- <el-input v-model="menuForm.icon" autocomplete="off" /> -->
          <el-select v-model="menuForm.icon" placeholder="请选择图标">
            <el-option v-for="item in iconlist" :key="item" :label="item" :value="item">
              <el-icon>
                <component :is="item"></component>
              </el-icon>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-else label="菜单地址" :label-width="formLabelWidth">
          <el-input v-model="menuForm.url" autocomplete="off" />
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-tooltip :content="'Switch value: ' + value" placement="top">
            <el-switch
              v-model="menuForm.status"
              style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ccc"
              :active-value="1"
              :inactive-value="2"
            />
          </el-tooltip>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancel(menuRef)">取消</el-button>
          <el-button type="primary" :plain="true" @click="confirm(menuRef)">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
const formLabelWidth = "140px";
import { ElMessage } from "element-plus";
import { defineProps, defineEmits, ref, reactive } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { menuadd, menuinfo, menuedit } from "../../request/api";
import { useStore } from "vuex";
let store = useStore();
const value = ref("1");
let props = defineProps({
  isShow: Boolean,
  isAdd: Boolean,
});
// icon 图标
const iconlist = ["Tools", "GoodsFilled", "Handbag", "Setting"];
// console.log(props,'传递过来的数据');
// 双向数据绑定的数据
let menuForm = ref({
  pid: 0, // 上级分类编号  注意：顶级菜单-0number
  title: "", //菜单名称string
  icon: "", //图标string
  type: 1, //类型 1目录2菜单number
  url: "", //菜单地址string
  status: 1, //状态   1正常2禁用   number类型number
});

//定义 rules规则
let rules = reactive<FormRules>({
  title: [
    { required: true, message: "请输入菜单名称", trigger: "blur" },
    { min: 2, max: 6, message: "Length should be 2 to 6", trigger: "blur" },
  ],
  pid: [
    {
      required: true,
      message: "请选择上级菜单",
      trigger: "change",
    },
  ],
});

// 调用子传父的事件触发
let emit = defineEmits(["cancel"]);
let menuRef = ref<FormInstance>();
// 封装一个取消事件
const cancel = (formEl?: FormInstance | undefined) => {
  emit("cancel", false);
  menuForm.value = {
    pid: 0, // 上级分类编号  注意：顶级菜单-0number
    title: "", //菜单名称string
    icon: "", //图标string
    type: 1, //类型 1目录2菜单number
    url: "", //菜单地址string
    status: 1, //状态   1正常2禁用   number类型number
  };
  if (!formEl) return;
  // 清除规则验证
  menuRef.value.resetFields();
};
// 数据回显
const lookup = async (id: number) => {
  let result: any = await menuinfo({ id });
  // console.log(result);
  if (result.code == 200) {
    menuForm.value = result.list;
    // 修改的时候需要id
    menuForm.value.id = id;
  }
};
// 封装一个确定事件
const confirm = async (formEl: FormInstance | undefined) => {
  //   console.log(menuForm.value, "表单对象");
  if (!formEl) return;
  await formEl.validate((val) => {
    if (val) {
      // console.log(menuForm.value, "表单对象");
      // console.log(menuForm,121212);
      if (props.isAdd) {
        menuadd(menuForm.value).then((res) => {
          // console.log(res);
          if (res.code == 200) {
            ElMessage({
              showClose: true,
              message: res.msg,
              type: "success",
            });
            cancel();
          } else {
            ElMessage({
              showClose: true,
              message: res.msg,
              type: "error",
            });
          }
          store.dispatch("menu/getMenuActions");
        });
      } else {
        menuedit(menuForm.value).then((res) => {
          // console.log(res);
          if (res.code == 200) {
            ElMessage({
              showClose: true,
              message: res.msg,
              type: "success",
            });
            cancel();
          } else {
            ElMessage({
              showClose: true,
              message: res.msg,
              type: "error",
            });
          }
          store.dispatch("menu/getMenuActions");
        });
      }
    }
  });
};
// 可以通过defineExpose 编译器宏来显示指定在 <script setup> 组件中要暴露出去的属性
defineExpose({
  lookup,
});
</script>

<style lang="less" scoped></style>
